<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="海报主图" prop="posterImg">
          <!-- <el-input v-model="dataForm.posterImg" placeholder="海报主图"></el-input> -->
          <el-upload
              class="avatar-uploader"
              :action="url"
              :show-file-list="false"
              :before-upload="beforeUploadHandle"
              :on-success="successHandle">
              <img v-if="posterImg" :src="posterImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
      </el-form-item>
          <el-form-item label="海报Logo" prop="logo">
          <!-- <el-input v-model="dataForm.logo" placeholder="海报显示平台logo"></el-input> -->
          <el-upload
              class="avatar-uploader"
              :action="url"
              :show-file-list="false"
              :before-upload="beforeUploadHandle"
              :on-success="successHandlelogo">
              <img v-if="logo" :src="logo" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
      </el-form-item>
          <el-form-item label="海报分享标题" prop="title">
          <el-input v-model="dataForm.title" placeholder="海报分享标题"></el-input>
      </el-form-item>
          <el-form-item label="海报分享内容" prop="content">
          <el-input v-model="dataForm.content" placeholder="海报分享内容"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import debounce from 'lodash/debounce'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      visible: false,
      logo: '',
      url: '',
      posterImg: '',
      dataForm: {
        id: '',
        posterImg: '',
        logo: '',
        title: '',
        content: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        posterImg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        logo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        title: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.url = `${window.SITE_CONFIG['apiURL']}/sys/oss/upload?token=${Cookies.get('token')}`
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 上传之前
    beforeUploadHandle (file) {
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
    },
        // 上传成功
    successHandle (res, file) {
      if(res.code == 0){
        this.posterImg = res.data.src,
        this.dataForm.posterImg = res.data.src
      }else{
        return this.$message.error(res.msg)
      }
    },
    successHandlelogo (res, file) {
      console.log(res)
      if(res.code == 0){
        this.logo = res.data.src,
        this.dataForm.logo = res.data.src
      }else{
        return this.$message.error(res.msg)
      }
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/poster/poster/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.posterImg = this.dataForm.posterImg
        this.logo = this.dataForm.logo
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/poster/poster/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
